D:\git\skunkworks\herald-for-cpp\herald-tests\data-tests.cpp
Line | Count | Source |
1 | | // Copyright 2020-2021 Herald Project Contributors |
2 | | // SPDX-License-Identifier: Apache-2.0 |
3 | | // |
4 | | |
5 | | #include <memory> |
6 | | #include <vector> |
7 | | #include <iostream> |
8 | | |
9 | | #include "catch.hpp" |
10 | | |
11 | | #include "herald/herald.h" |
12 | | |
13 | 1 | TEST_CASE("datatypes-data-ctor-empty", "[datatypes][data][ctor][empty]") { |
14 | 1 | SECTION("datatypes-data-ctor-empty") { |
15 | 1 | herald::datatype::Data data; |
16 | 1 | REQUIRE(data.size() == 0); |
17 | 1 | REQUIRE(data.hexEncodedString() == ""); |
18 | 1 | } |
19 | 1 | } |
20 | | |
21 | 1 | TEST_CASE("datatypes-data-ctor-rawarray", "[datatypes][data][ctor][rawarray]") { |
22 | 1 | SECTION("datatypes-data-ctor-rawarray") { |
23 | 1 | std::uint8_t rawData[] {0,1,5,6,7,8,12,13,14}; |
24 | 1 | herald::datatype::Data data(rawData,9); |
25 | 1 | REQUIRE(data.size() == 9); |
26 | 1 | REQUIRE(data.at(0) == std::byte(0)); |
27 | 1 | REQUIRE(data.at(1) == std::byte(1)); |
28 | 1 | REQUIRE(data.at(2) == std::byte(5)); |
29 | 1 | REQUIRE(data.at(3) == std::byte(6)); |
30 | 1 | REQUIRE(data.at(4) == std::byte(7)); |
31 | 1 | REQUIRE(data.at(5) == std::byte(8)); |
32 | 1 | REQUIRE(data.at(6) == std::byte(12)); |
33 | 1 | REQUIRE(data.at(7) == std::byte(13)); |
34 | 1 | REQUIRE(data.at(8) == std::byte(14)); |
35 | 1 | } |
36 | 1 | } |
37 | | |
38 | 1 | TEST_CASE("datatypes-data-ctor-move", "[datatypes][data][ctor][move]") { |
39 | 1 | SECTION("datatypes-data-ctor-move") { |
40 | 1 | const uint8_t bytes[] = {0,1,2,3}; |
41 | 1 | herald::datatype::Data orig{bytes, 4}; |
42 | 1 | herald::datatype::Data d(std::move(orig)); |
43 | 1 | |
44 | 1 | REQUIRE(d.size() == 4); |
45 | 1 | REQUIRE(d.at(0) == std::byte(0)); |
46 | 1 | REQUIRE(d.at(1) == std::byte(1)); |
47 | 1 | REQUIRE(d.at(2) == std::byte(2)); |
48 | 1 | REQUIRE(d.at(3) == std::byte(3)); |
49 | 1 | |
50 | 1 | REQUIRE(orig.size() == 0); |
51 | 1 | REQUIRE(orig.hexEncodedString() == ""); // this will definitely try to 'use' the underlying data store |
52 | 1 | } |
53 | 1 | } |
54 | | |
55 | 1 | TEST_CASE("datatypes-data-assign-move", "[datatypes][data][assign][move]") { |
56 | 1 | SECTION("datatypes-data-assign-move") { |
57 | 1 | const uint8_t bytes[] = {0,1,2,3}; |
58 | 1 | herald::datatype::Data orig{bytes, 4}; |
59 | 1 | herald::datatype::Data d = std::move(orig); |
60 | 1 | |
61 | 1 | REQUIRE(d.size() == 4); |
62 | 1 | REQUIRE(d.at(0) == std::byte(0)); |
63 | 1 | REQUIRE(d.at(1) == std::byte(1)); |
64 | 1 | REQUIRE(d.at(2) == std::byte(2)); |
65 | 1 | REQUIRE(d.at(3) == std::byte(3)); |
66 | 1 | |
67 | 1 | REQUIRE(orig.size() == 0); |
68 | 1 | REQUIRE(orig.hexEncodedString() == ""); // this will definitely try to 'use' the underlying data store |
69 | 1 | } |
70 | 1 | } |
71 | | |
72 | 1 | TEST_CASE("datatypes-data-ctor-copy", "[datatypes][data][ctor][copy]") { |
73 | 1 | SECTION("datatypes-data-ctor-copy") { |
74 | 1 | const uint8_t bytes[] = {0,1,2,3}; |
75 | 1 | const herald::datatype::Data orig{bytes, 4}; // const to ensure a copy |
76 | 1 | herald::datatype::Data d(orig); |
77 | 1 | |
78 | 1 | REQUIRE(d.size() == 4); |
79 | 1 | REQUIRE(d.at(0) == std::byte(0)); |
80 | 1 | REQUIRE(d.at(1) == std::byte(1)); |
81 | 1 | REQUIRE(d.at(2) == std::byte(2)); |
82 | 1 | REQUIRE(d.at(3) == std::byte(3)); |
83 | 1 | |
84 | 1 | REQUIRE(orig.size() == 4); |
85 | 1 | } |
86 | 1 | } |
87 | | |
88 | 1 | TEST_CASE("datatypes-data-assign-copy", "[datatypes][data][assign][copy]") { |
89 | 1 | SECTION("datatypes-data-assign-copy") { |
90 | 1 | const uint8_t bytes[] = {0,1,2,3}; |
91 | 1 | const herald::datatype::Data orig{bytes, 4}; // const to ensure a copy |
92 | 1 | herald::datatype::Data d = orig; |
93 | 1 | |
94 | 1 | REQUIRE(d.size() == 4); |
95 | 1 | REQUIRE(d.at(0) == std::byte(0)); |
96 | 1 | REQUIRE(d.at(1) == std::byte(1)); |
97 | 1 | REQUIRE(d.at(2) == std::byte(2)); |
98 | 1 | REQUIRE(d.at(3) == std::byte(3)); |
99 | 1 | |
100 | 1 | REQUIRE(orig.size() == 4); |
101 | 1 | } |
102 | 1 | } |
103 | | |
104 | 1 | TEST_CASE("datatypes-data-from-bytearray", "[datatypes][data][ctor][from-bytearray]") { |
105 | 1 | SECTION("datatypes-data-from-bytearray") { |
106 | 1 | const std::byte bytes[] = {std::byte(0),std::byte(1),std::byte(2),std::byte(3)}; |
107 | 1 | herald::datatype::Data d{bytes, 4}; |
108 | 1 | |
109 | 1 | REQUIRE(d.size() == 4); |
110 | 1 | REQUIRE(d.at(0) == std::byte(0)); |
111 | 1 | REQUIRE(d.at(1) == std::byte(1)); |
112 | 1 | REQUIRE(d.at(2) == std::byte(2)); |
113 | 1 | REQUIRE(d.at(3) == std::byte(3)); |
114 | 1 | } |
115 | 1 | } |
116 | | |
117 | 1 | TEST_CASE("datatypes-data-from-uint8array", "[datatypes][data][ctor][from-uint8array]") { |
118 | 1 | SECTION("datatypes-data-from-uint8array") { |
119 | 1 | const uint8_t bytes[] = {0,1,2,3}; |
120 | 1 | herald::datatype::Data d{bytes, 4}; |
121 | 1 | |
122 | 1 | std::string hs = d.hexEncodedString(); |
123 | 1 | INFO("Data: uint8array as hexString: expected: 00010203, got: " << hs); |
124 | 1 | |
125 | 1 | REQUIRE(d.size() == 4); |
126 | 1 | REQUIRE("00010203" == hs); |
127 | 1 | REQUIRE(d.at(0) == std::byte(0)); |
128 | 1 | REQUIRE(d.at(1) == std::byte(1)); |
129 | 1 | REQUIRE(d.at(2) == std::byte(2)); |
130 | 1 | REQUIRE(d.at(3) == std::byte(3)); |
131 | 1 | } |
132 | 1 | } |
133 | | |
134 | 1 | TEST_CASE("datatypes-data-from-vector", "[datatypes][data][from-vector]") { |
135 | 1 | SECTION("datatypes-data-from-vector") { |
136 | 1 | herald::datatype::Base64String str; |
137 | 1 | // String is 'wotcha' |
138 | 1 | bool encodeOk = herald::datatype::Base64String::from("d290Y2hh",str); |
139 | 1 | herald::datatype::Data data = str.decode(); |
140 | 1 | REQUIRE(encodeOk); |
141 | 1 | REQUIRE(data.size() == 6); |
142 | 1 | REQUIRE(data.at(0) == std::byte('w')); |
143 | 1 | REQUIRE(data.at(1) == std::byte('o')); |
144 | 1 | REQUIRE(data.at(5) == std::byte('a')); |
145 | 1 | |
146 | 1 | data.append(data); |
147 | 1 | REQUIRE(data.size() == 12); |
148 | 1 | REQUIRE(data.at(0) == std::byte('w')); |
149 | 1 | REQUIRE(data.at(5) == std::byte('a')); |
150 | 1 | REQUIRE(data.at(6) == std::byte('w')); |
151 | 1 | REQUIRE(data.at(11) == std::byte('a')); |
152 | 1 | |
153 | 1 | auto d2 = data.subdata(3,6); |
154 | 1 | herald::datatype::Base64String expStr; |
155 | 1 | bool d2EncodeOk = herald::datatype::Base64String::from("Y2hhd290",expStr); // chawot |
156 | 1 | herald::datatype::Data expData = expStr.decode(); |
157 | 1 | REQUIRE(d2.size() == 6); |
158 | 1 | REQUIRE(d2 == expData); |
159 | 1 | REQUIRE(d2.at(0) == std::byte('c')); |
160 | 1 | REQUIRE(d2.at(5) == std::byte('t')); |
161 | 1 | |
162 | 1 | auto d3 = d2.subdata(3); |
163 | 1 | REQUIRE(d3.size() == 3); |
164 | 1 | REQUIRE(d3.at(0) == std::byte('w')); |
165 | 1 | REQUIRE(d3.at(2) == std::byte('t')); |
166 | 1 | } |
167 | 1 | } |
168 | | |
169 | 1 | TEST_CASE("datatypes-data-ctor-repeat", "[datatypes][data][ctor][repeat]") { |
170 | 1 | SECTION("datatypes-data-ctor-repeat") { |
171 | 1 | herald::datatype::Data d{std::byte('a'),6}; |
172 | 1 | |
173 | 1 | REQUIRE(d.size() == 6); |
174 | 1 | REQUIRE(d.at(0) == std::byte('a')); |
175 | 1 | REQUIRE(d.at(5) == std::byte('a')); |
176 | 1 | } |
177 | 1 | } |
178 | | |
179 | 1 | TEST_CASE("datatypes-data-ctor-fromhexstring", "[datatypes][data][ctor][fromhexstring]") { |
180 | 1 | SECTION("datatypes-data-ctor-fromhexstring") { |
181 | 1 | const std::string hex = "00010ff0ffcc"; |
182 | 1 | herald::datatype::Data d = herald::datatype::Data::fromHexEncodedString(hex); |
183 | 1 | const std::string finalhex = d.hexEncodedString(); |
184 | 1 | INFO("Data: fromHexEncodedString: from: " << hex << ", to: " << finalhex); |
185 | 1 | |
186 | 1 | REQUIRE(d.size() == 6); |
187 | 1 | REQUIRE(d.at(0) == std::byte(0)); |
188 | 1 | REQUIRE(d.at(1) == std::byte(1)); |
189 | 1 | REQUIRE(std::size_t(std::uint8_t(0x0f)) == std::size_t(15)); |
190 | 1 | // REQUIRE(std::uint8_t(0x0f) == std::uint8_t('f')); |
191 | 1 | REQUIRE(d.at(2) == std::byte(15)); |
192 | 1 | REQUIRE(d.at(3) == std::byte(240)); |
193 | 1 | REQUIRE(d.at(4) == std::byte(255)); |
194 | 1 | REQUIRE(d.at(5) == std::byte(204)); |
195 | 1 | } |
196 | 1 | } |
197 | | |
198 | | // TODO uppercase fex encoding test |
199 | | // TODO mixedcase fex encoding test |
200 | | // TODO hex string with odd number of chars (leading 0 removed) |
201 | | // TODO invalid chars in hex string |
202 | | |
203 | 1 | TEST_CASE("datatypes-data-append", "[datatypes][data][append]") { |
204 | 1 | SECTION("datatypes-data-append") { |
205 | 1 | herald::datatype::Data d{std::byte('a'),6}; // 6 |
206 | 1 | const uint8_t u8 = 23; // 1 |
207 | 1 | const uint16_t u16 = 65530; // 2 |
208 | 1 | const uint32_t u32 = 122345; // 4 |
209 | 1 | const uint64_t u64 = 4295967296; // 8 |
210 | 1 | const std::string s = "lorem ipsum dolar Sit Amet Consecutor"; // 37 |
211 | 1 | uint8_t byteArray[] = {0,1,2,3,4,5,6,7,8}; |
212 | 1 | d.append(u8); |
213 | 1 | d.append(u16); |
214 | 1 | d.append(u32); |
215 | 1 | d.append(u64); |
216 | 1 | d.append(s); |
217 | 1 | d.append(byteArray, 1, 6); |
218 | 1 | |
219 | 1 | REQUIRE(d.size() == 64); |
220 | 1 | REQUIRE(d.at(0) == std::byte('a')); |
221 | 1 | REQUIRE(d.at(5) == std::byte('a')); |
222 | 1 | REQUIRE(d.at(6) == std::byte(uint8_t(23))); |
223 | 1 | REQUIRE(d.at(57) == std::byte('r')); |
224 | 1 | REQUIRE(d.at(58) == std::byte(1)); |
225 | 1 | REQUIRE(d.at(63) == std::byte(6)); |
226 | 1 | |
227 | 1 | // now ensure that byte order is correct |
228 | 1 | uint8_t r8 = 0; |
229 | 1 | uint16_t r16 = 0; |
230 | 1 | uint32_t r32 = 0; |
231 | 1 | uint64_t r64 = 0; |
232 | 1 | bool r8ok = d.uint8(6,r8); |
233 | 1 | bool r16ok = d.uint16(7,r16); |
234 | 1 | bool r32ok = d.uint32(9,r32); |
235 | 1 | bool r64ok = d.uint64(13,r64); |
236 | 1 | REQUIRE(r8ok); |
237 | 1 | REQUIRE(r16ok); |
238 | 1 | REQUIRE(r32ok); |
239 | 1 | REQUIRE(r64ok); |
240 | 1 | REQUIRE(r8 == u8); |
241 | 1 | REQUIRE(r16 == u16); |
242 | 1 | REQUIRE(d.at(9) == std::byte(0xe9)); |
243 | 1 | REQUIRE(d.at(10) == std::byte(0xdd)); |
244 | 1 | REQUIRE(d.at(11) == std::byte(0x01)); |
245 | 1 | REQUIRE(d.at(12) == std::byte(0x00)); |
246 | 1 | REQUIRE(r32 == u32); |
247 | 1 | REQUIRE(d.at(20) == std::byte(0x00)); |
248 | 1 | REQUIRE(d.at(19) == std::byte(0x00)); |
249 | 1 | REQUIRE(d.at(18) == std::byte(0x00)); |
250 | 1 | REQUIRE(d.at(17) == std::byte(0x01)); |
251 | 1 | REQUIRE(d.at(16) == std::byte(0x00)); |
252 | 1 | REQUIRE(d.at(15) == std::byte(0x0f)); |
253 | 1 | REQUIRE(d.at(14) == std::byte(0x42)); |
254 | 1 | REQUIRE(d.at(13) == std::byte(0x40)); |
255 | 1 | REQUIRE(r64 == u64); |
256 | 1 | } |
257 | 1 | } |
258 | | |
259 | 1 | TEST_CASE("datatypes-data-append-data", "[datatypes][data][append-data]") { |
260 | 1 | SECTION("datatypes-data-append-data") { |
261 | 1 | herald::datatype::Data d{std::byte('a'),6}; // 6 |
262 | 1 | herald::datatype::Data dAdd{std::byte(8),1}; // 1 |
263 | 1 | |
264 | 1 | d.append(dAdd); |
265 | 1 | |
266 | 1 | REQUIRE(d.size() == 7); |
267 | 1 | std::uint8_t b; |
268 | 1 | bool ok = d.uint8(6, b); |
269 | 1 | REQUIRE(ok); |
270 | 1 | REQUIRE(std::uint8_t(8) == b); |
271 | 1 | } |
272 | 1 | } |
273 | | |
274 | 1 | TEST_CASE("datatypes-data-reversed", "[datatypes][data][reversed]") { |
275 | 1 | SECTION("datatypes-data-reversed") { |
276 | 1 | const uint8_t bytes[] = {0,1,2,3}; |
277 | 1 | herald::datatype::Data d{bytes, 4}; |
278 | 1 | |
279 | 1 | REQUIRE(d.size() == 4); |
280 | 1 | REQUIRE(d.at(0) == std::byte(0)); |
281 | 1 | REQUIRE(d.at(1) == std::byte(1)); |
282 | 1 | REQUIRE(d.at(2) == std::byte(2)); |
283 | 1 | REQUIRE(d.at(3) == std::byte(3)); |
284 | 1 | |
285 | 1 | herald::datatype::Data rev = d.reversed(); |
286 | 1 | REQUIRE(rev.size() == 4); |
287 | 1 | REQUIRE(rev.at(0) == std::byte(3)); |
288 | 1 | REQUIRE(rev.at(1) == std::byte(2)); |
289 | 1 | REQUIRE(rev.at(2) == std::byte(1)); |
290 | 1 | REQUIRE(rev.at(3) == std::byte(0)); |
291 | 1 | } |
292 | 1 | } |
293 | | |
294 | 1 | TEST_CASE("datatypes-data-changeendianness", "[datatypes][data][changeendianness]") { |
295 | 1 | SECTION("datatypes-data-changeendianness") { |
296 | 1 | const std::byte fifteen = std::byte(15); |
297 | 1 | |
298 | 1 | herald::datatype::Data d{fifteen,5}; |
299 | 1 | |
300 | 1 | const std::uint8_t uintFifteen = 15; // 0f |
301 | 1 | const std::uint8_t uintTwoFourty = 240; // f0 |
302 | 1 | |
303 | 1 | std::uint8_t value; |
304 | 1 | |
305 | 6 | for (std::size_t i = 0;i < 5;++i5 ) { |
306 | 5 | REQUIRE(d.uint8(i, value)); |
307 | 5 | REQUIRE(uintFifteen == value); |
308 | 5 | } |
309 | 1 | |
310 | 1 | herald::datatype::Data rev = d.reverseEndianness(); |
311 | 1 | |
312 | 1 | REQUIRE(d.size() == 5); |
313 | 1 | REQUIRE(rev.size() == 5); |
314 | 1 | |
315 | 6 | for (std::size_t i = 0;1 i < 5;++i5 ) { |
316 | 5 | REQUIRE(rev.uint8(i, value)); |
317 | 5 | INFO("Byte value is " << value << " with hex " << rev.subdata(i,1).hexEncodedString()); |
318 | 5 | REQUIRE(uintTwoFourty == value); |
319 | 5 | } |
320 | 1 | } |
321 | 1 | } |
322 | | |
323 | 1 | TEST_CASE("datatypes-data-description", "[datatypes][data][description]") { |
324 | 1 | SECTION("datatypes-data-description") { |
325 | 1 | const uint8_t bytes[] = {0,1,2,3}; |
326 | 1 | herald::datatype::Data d{bytes, 4}; |
327 | 1 | |
328 | 1 | std::string hex = d.description(); |
329 | 1 | INFO("Data: description output: " << hex); |
330 | 1 | REQUIRE(hex.size() > 0); |
331 | 1 | // NOTE: No requirements on format for this method - DO NOT rely on it |
332 | 1 | } |
333 | 1 | } |
334 | | |
335 | 1 | TEST_CASE("datatypes-data-hexencodedstring", "[datatypes][data][hexencodedstring]") { |
336 | 1 | SECTION("datatypes-data-hexencodedstring") { |
337 | 1 | const uint8_t bytes[] = {0,1,2,3}; |
338 | 1 | herald::datatype::Data d{bytes, 4}; |
339 | 1 | |
340 | 1 | std::string hex = d.hexEncodedString(); |
341 | 1 | INFO("Data: hexEncodedString (std::string) output: " << hex); |
342 | 1 | REQUIRE(8 == hex.size()); |
343 | 1 | REQUIRE("00010203" == hex); |
344 | 1 | |
345 | 1 | std::string hexrev = d.reversed().hexEncodedString(); |
346 | 1 | INFO("Data: hexEncodedString (std::string) reversed output: " << hexrev); |
347 | 1 | REQUIRE(8 == hexrev.size()); |
348 | 1 | REQUIRE("03020100" == hexrev); |
349 | 1 | } |
350 | 1 | } |
351 | | |
352 | | |
353 | | |
354 | 1 | TEST_CASE("datatypes-data-subdata-offset-valid", "[datatypes][data][subdata][offset][valid]") { |
355 | 1 | SECTION("datatypes-data-subdata-offset-valid") { |
356 | 1 | const uint8_t bytes[] = {0,1,2,3,4,5,6,7}; |
357 | 1 | herald::datatype::Data d{bytes, 8}; |
358 | 1 | |
359 | 1 | herald::datatype::Data s = d.subdata(4); |
360 | 1 | REQUIRE(s.size() == 4); |
361 | 1 | REQUIRE(s.at(0) == std::byte(4)); |
362 | 1 | REQUIRE(s.at(1) == std::byte(5)); |
363 | 1 | REQUIRE(s.at(2) == std::byte(6)); |
364 | 1 | REQUIRE(s.at(3) == std::byte(7)); |
365 | 1 | } |
366 | 1 | } |
367 | | |
368 | 1 | TEST_CASE("datatypes-data-subdata-offset-toohigh", "[datatypes][data][subdata][offset][invalid][toohigh]") { |
369 | 1 | SECTION("datatypes-data-subdata-offset-toohigh") { |
370 | 1 | const uint8_t bytes[] = {0,1,2,3,4,5,6,7}; |
371 | 1 | herald::datatype::Data d{bytes, 8}; |
372 | 1 | |
373 | 1 | herald::datatype::Data s = d.subdata(8); |
374 | 1 | REQUIRE(s.size() == 0); |
375 | 1 | } |
376 | 1 | } |
377 | | |
378 | 1 | TEST_CASE("datatypes-data-subdata-offset-toolow", "[datatypes][data][subdata][offset][invalid][toolow]") { |
379 | 1 | SECTION("datatypes-data-subdata-offset-toolow") { |
380 | 1 | const uint8_t bytes[] = {0,1,2,3,4,5,6,7}; |
381 | 1 | herald::datatype::Data d{bytes, 8}; |
382 | 1 | |
383 | 1 | herald::datatype::Data s = d.subdata(-1); // std::size_t is signed, but -1 makes a very high size, so should be tested |
384 | 1 | REQUIRE(s.size() == 0); |
385 | 1 | } |
386 | 1 | } |
387 | | |
388 | | |
389 | | |
390 | 1 | TEST_CASE("datatypes-data-subdata-length-valid", "[datatypes][data][subdata][length][valid]") { |
391 | 1 | SECTION("datatypes-data-subdata-length-valid") { |
392 | 1 | const uint8_t bytes[] = {0,1,2,3,4,5,6,7}; |
393 | 1 | herald::datatype::Data d{bytes, 8}; |
394 | 1 | |
395 | 1 | herald::datatype::Data s = d.subdata(4, 3); |
396 | 1 | REQUIRE(s.size() == 3); |
397 | 1 | REQUIRE(s.at(0) == std::byte(4)); |
398 | 1 | REQUIRE(s.at(1) == std::byte(5)); |
399 | 1 | REQUIRE(s.at(2) == std::byte(6)); |
400 | 1 | } |
401 | 1 | } |
402 | | |
403 | 1 | TEST_CASE("datatypes-data-subdata-length-toohigh", "[datatypes][data][subdata][length][toohigh]") { |
404 | 1 | SECTION("datatypes-data-subdata-length-toohigh") { |
405 | 1 | const uint8_t bytes[] = {0,1,2,3,4,5,6,7}; |
406 | 1 | herald::datatype::Data d{bytes, 8}; |
407 | 1 | |
408 | 1 | herald::datatype::Data s = d.subdata(4, 5); |
409 | 1 | REQUIRE(s.size() == 4); |
410 | 1 | REQUIRE(s.at(0) == std::byte(4)); |
411 | 1 | REQUIRE(s.at(1) == std::byte(5)); |
412 | 1 | REQUIRE(s.at(2) == std::byte(6)); |
413 | 1 | REQUIRE(s.at(3) == std::byte(7)); |
414 | 1 | } |
415 | 1 | } |
416 | | |
417 | 1 | TEST_CASE("datatypes-data-subdata-length-toolow", "[datatypes][data][subdata][length][toolow]") { |
418 | 1 | SECTION("datatypes-data-subdata-length-toolow") { |
419 | 1 | const uint8_t bytes[] = {0,1,2,3,4,5,6,7}; |
420 | 1 | herald::datatype::Data d{bytes, 8}; |
421 | 1 | |
422 | 1 | herald::datatype::Data s = d.subdata(4, -1); // std::size_t is signed, but -1 makes a very high size, so should be tested |
423 | 1 | REQUIRE(s.size() == 4); // no effective way to tell the difference between this and too high length |
424 | 1 | REQUIRE(s.at(0) == std::byte(4)); |
425 | 1 | REQUIRE(s.at(1) == std::byte(5)); |
426 | 1 | REQUIRE(s.at(2) == std::byte(6)); |
427 | 1 | REQUIRE(s.at(3) == std::byte(7)); |
428 | 1 | } |
429 | 1 | } |
430 | | |
431 | 1 | TEST_CASE("datatypes-data-subdata-length-zero", "[datatypes][data][subdata][length][zero]") { |
432 | 1 | SECTION("datatypes-data-subdata-length-zero") { |
433 | 1 | const uint8_t bytes[] = {0,1,2,3,4,5,6,7}; |
434 | 1 | herald::datatype::Data d{bytes, 8}; |
435 | 1 | |
436 | 1 | herald::datatype::Data s = d.subdata(4, 0); |
437 | 1 | REQUIRE(s.size() == 0); |
438 | 1 | } |
439 | 1 | } |
440 | | |
441 | | |
442 | | |
443 | 1 | TEST_CASE("datatypes-data-at-valid", "[datatypes][data][at][valid]") { |
444 | 1 | SECTION("datatypes-data-at-valid") { |
445 | 1 | const uint8_t bytes[] = {0,1,2,3,4,5,6,7}; |
446 | 1 | herald::datatype::Data d{bytes, 8}; |
447 | 1 | |
448 | 1 | REQUIRE(d.at(3) == std::byte(3)); |
449 | 1 | } |
450 | 1 | } |
451 | | |
452 | 1 | TEST_CASE("datatypes-data-at-toohigh", "[datatypes][data][at][invalid][toohigh") { |
453 | 1 | SECTION("datatypes-data-at-toohigh") { |
454 | 1 | const uint8_t bytes[] = {0,1,2,3,4,5,6,7}; |
455 | 1 | herald::datatype::Data d{bytes, 8}; |
456 | 1 | |
457 | 1 | REQUIRE(d.at(8) == std::byte(0)); |
458 | 1 | } |
459 | 1 | } |
460 | | |
461 | 1 | TEST_CASE("datatypes-data-at-toolow", "[datatypes][data][at][invalid][toolow") { |
462 | 1 | SECTION("datatypes-data-at-toolow") { |
463 | 1 | const uint8_t bytes[] = {0,1,2,3,4,5,6,7}; |
464 | 1 | herald::datatype::Data d{bytes, 8}; |
465 | 1 | |
466 | 1 | REQUIRE(d.at(-1) == std::byte(0)); // -1 and LONG_LONG_MAX are the same, so should be same as toohigh |
467 | 1 | } |
468 | 1 | } |
469 | | |
470 | | |
471 | 1 | TEST_CASE("datatypes-data-extract-valid", "[datatypes][data][extract][valid]") { |
472 | 1 | SECTION("datatypes-data-extract-valid") { |
473 | 1 | // REMEMBER Data uses little endian for encoding/decoding of individual types |
474 | 1 | // OUTER byte order big endian, data byte order little endian |
475 | 1 | const uint8_t bytes[] = { 0, 1, |
476 | 1 | 0x7C, 0x02, // 636 as unsigned int16 in hex |
477 | 1 | 0x89, 0x90, // 37001 as usigned int16 |
478 | 1 | 0xb3, 0xb5, 0x56, 0x07, // 123123123 as uint32 // NB last byte also used in uint64 decoding |
479 | 1 | 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
480 | 1 | 0xff // extra data at end to ensure uint64 is not off by one |
481 | 1 | }; |
482 | 1 | herald::datatype::Data d{bytes, 18}; |
483 | 1 | |
484 | 1 | uint8_t u8 = 0; |
485 | 1 | bool u8ok = d.uint8(0, u8); |
486 | 1 | REQUIRE(u8ok); |
487 | 1 | REQUIRE(u8 == 0x00); |
488 | 1 | |
489 | 1 | u8ok = d.uint8(1, u8); |
490 | 1 | REQUIRE(u8ok); |
491 | 1 | REQUIRE(u8 == 0x01); |
492 | 1 | |
493 | 1 | u8ok = d.uint8(17, u8); |
494 | 1 | REQUIRE(u8ok); |
495 | 1 | REQUIRE(u8 == 0xff); |
496 | 1 | |
497 | 1 | uint16_t u16 = 0; |
498 | 1 | bool u16ok = d.uint16(2, u16); |
499 | 1 | REQUIRE(u16ok); |
500 | 1 | REQUIRE(u16 == 636); |
501 | 1 | |
502 | 1 | u16 = 0; |
503 | 1 | u16ok = d.uint16(4, u16); |
504 | 1 | REQUIRE(u16ok); |
505 | 1 | REQUIRE(u16 == 37001); |
506 | 1 | |
507 | 1 | uint32_t u32 = 0; |
508 | 1 | bool u32ok = d.uint32(6, u32); |
509 | 1 | REQUIRE(u32ok); |
510 | 1 | REQUIRE(u32 == 123123123); |
511 | 1 | |
512 | 1 | uint64_t u64 = 0; |
513 | 1 | bool u64ok = d.uint64(9, u64); |
514 | 1 | REQUIRE(u64ok); |
515 | 1 | REQUIRE(u64 == 0xffffffffffffff07); |
516 | 1 | } |
517 | 1 | } |
518 | | |
519 | | |
520 | 1 | TEST_CASE("datatypes-data-append-bytearray-valid", "[datatypes][data][append][bytearray][valid]") { |
521 | 1 | SECTION("datatypes-data-append-bytearray-valid") { |
522 | 1 | uint8_t initial[] = {0,1,2,3,4,5,6,7}; |
523 | 1 | herald::datatype::Data d(initial, 8); |
524 | 1 | uint8_t byteArray[] = {0,1,2,3,4,5,6,7}; |
525 | 1 | |
526 | 1 | d.append(byteArray,0,8); |
527 | 1 | REQUIRE(d.size() == 16); |
528 | 1 | REQUIRE(d.at(8) == std::byte(0)); |
529 | 1 | REQUIRE(d.at(15) == std::byte(7)); |
530 | 1 | } |
531 | 1 | } |
532 | | |
533 | | |
534 | 1 | TEST_CASE("datatypes-data-append-data-valid", "[datatypes][data][append][data][valid]") { |
535 | 1 | SECTION("datatypes-data-append-data-valid") { |
536 | 1 | uint8_t initial[] = {0,1,2,3,4,5,6,7}; |
537 | 1 | herald::datatype::Data d(initial, 8); |
538 | 1 | uint8_t byteArray[] = {0,1,2,3,4,5,6,7}; |
539 | 1 | herald::datatype::Data more(byteArray,8); |
540 | 1 | |
541 | 1 | d.append(more); |
542 | 1 | REQUIRE(d.size() == 16); |
543 | 1 | REQUIRE(d.at(8) == std::byte(0)); |
544 | 1 | REQUIRE(d.at(15) == std::byte(7)); |
545 | 1 | } |
546 | 1 | } |
547 | | |
548 | | |
549 | 1 | TEST_CASE("datatypes-data-append-data-reversed-valid", "[datatypes][data][append][data][reversed][valid]") { |
550 | 1 | SECTION("datatypes-data-append-data-reversed-valid") { |
551 | 1 | uint8_t initial[] = {0,1,2,3,4,5,6,7}; |
552 | 1 | herald::datatype::Data d(initial, 8); |
553 | 1 | uint8_t byteArray[] = {0,1,2,3,4,5,6,7}; |
554 | 1 | herald::datatype::Data more(byteArray,8); |
555 | 1 | |
556 | 1 | d.append(more.reversed()); |
557 | 1 | REQUIRE(d.size() == 16); |
558 | 1 | REQUIRE(d.at(8) == std::byte(7)); |
559 | 1 | REQUIRE(d.at(15) == std::byte(0)); |
560 | 1 | } |
561 | 1 | } |
562 | | |
563 | | |
564 | 1 | TEST_CASE("datatypes-data-append-data-reversed-mthd-valid", "[datatypes][data][append][data][reversed][mthd][valid]") { |
565 | 1 | SECTION("datatypes-data-append-data-reversed-mthd-valid") { |
566 | 1 | uint8_t initial[] = {0,1,2,3,4,5,6,7}; |
567 | 1 | herald::datatype::Data d(initial, 8); |
568 | 1 | uint8_t byteArray[] = {0,1,2,3,4,5,6,7}; |
569 | 1 | herald::datatype::Data more(byteArray,8); |
570 | 1 | |
571 | 1 | d.appendReversed(more,0,more.size()); |
572 | 1 | REQUIRE(d.size() == 16); |
573 | 1 | REQUIRE(d.at(8) == std::byte(7)); |
574 | 1 | REQUIRE(d.at(15) == std::byte(0)); |
575 | 1 | } |
576 | 1 | } |
577 | | |
578 | | |
579 | | |
580 | 1 | TEST_CASE("datatypes-data-equals", "[datatypes][data][equals]") { |
581 | 1 | SECTION("datatypes-data-equals") { |
582 | 1 | uint8_t initial[] = {0,1,2,3,4,5,6,7}; |
583 | 1 | herald::datatype::Data d1(initial, 8); |
584 | 1 | |
585 | 1 | uint8_t byteArray[] = {0,1,2,3,4,5,6,7}; |
586 | 1 | herald::datatype::Data d2(byteArray,8); |
587 | 1 | uint8_t byteArray2[] = {4,4,4,4,4,4,4,4}; |
588 | 1 | herald::datatype::Data d3(byteArray2,8); |
589 | 1 | |
590 | 1 | REQUIRE(d1.size() == d2.size()); |
591 | 1 | REQUIRE(d1.at(0) == d2.at(0)); |
592 | 1 | REQUIRE(d1.at(7) == d2.at(7)); |
593 | 1 | REQUIRE(d1.hashCode() == d2.hashCode()); |
594 | 1 | REQUIRE(false == (d2.hashCode() == d3.hashCode())); // tests operator== |
595 | 1 | REQUIRE(d2.hashCode() != d3.hashCode()); |
596 | 1 | REQUIRE(d3.hashCode() != d1.hashCode()); |
597 | 1 | REQUIRE(d1 == d2); |
598 | 1 | REQUIRE(false == (d1 == d3)); // tests operator== |
599 | 1 | REQUIRE(d1 != d3); // tests operator!= |
600 | 1 | REQUIRE(d2 != d3); |
601 | 1 | REQUIRE(d3 != d1); |
602 | 1 | |
603 | 1 | } |
604 | 1 | } |